/**
 * Copyright (c) 2011 Daniel Centore
 *
 * ex: set filetype=java expandtab tabstop=4 shiftwidth=4 :
 * 
 * This program is free software: you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation, either version 3 of
 * the License, or (at your option) any later version.
 *
 * This code is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * This code is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published
 * by the Free Software Foundation.
 */
package wiiWired.controller.data;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

/**
 * Class which handles the names of verious remotes
 * 
 * @author Daniel Centore
 * 
 */
public class NameGetter
{

	public static final String MOTES = "./motes.txt";

	public static final int BTA = 0;
	public static final int NAME = 1;

	private static List<btaName> btaNames = new ArrayList<btaName>(); //list of bluetooth names

	/**
	 * Returns the name of a remote
	 * @param bta The BT adress of the remote
	 * @return
	 */
	public static String getName(String bta)
	{
		String[] q = null;

		q = getMoteData(bta);

		try
		{
			return q[NAME];
		} catch (Exception e)
		{
			return bta; // default
		}
	}

	public synchronized static String[] getMoteData(String bta)
	{
		String s = "";

		Scanner scan;
		try
		{
			scan = new Scanner(new File(MOTES));
		} catch (FileNotFoundException e)
		{
			e.printStackTrace();
			return null;
		}

		while (scan.hasNext())
			s += scan.nextLine() + "\n";
		
		scan.close();

		String[] k = s.split("\n");

		for (String q : k)
		{
			if (q.startsWith(bta + ","))
				return q.split(",");
		}
		
		return null;
	}
	
	public synchronized static String[][] getData()
	{
		String s = "";

		Scanner scan;
		try
		{
			scan = new Scanner(new File(MOTES));
		} catch (FileNotFoundException e)
		{
			e.printStackTrace();
			return null;
		}

		while (scan.hasNext())
			s += scan.nextLine() + "\n";
		
		s.trim();
		
		scan.close();
		
		if (s == "")
			return new String[0][];

		String[] k = s.split("\n");
		String[][] r = new String[k.length][];
		int i = 0;
		for (String q : k)
		{
			r[i++] = q.split(",");
		}
		
		return r;
	}

	public static synchronized void writeToFile(String[][] data)
	{
		try
		{
			FileWriter fstream = new FileWriter(MOTES);
			BufferedWriter out = new BufferedWriter(fstream);
			
			String write = "";
			
			for (String[] k : data)
			{
				for (String s : k)
				{
					write += s + ",";
				}
				write = write.substring(0, write.length() - 1);
				write += "\n";
			}
			write = write.substring(0, write.length() - 1);
			
			out.write(write);

			out.close();
		} catch (IOException e)
		{
			e.printStackTrace();
		}
	}

}

class btaName
{

	String bta;
	String name;

	public btaName(String _bta, String _name)
	{
		bta = _bta;
		name = _name;
	}
}